package com.todod.common.util;


public class Algorithm {
    /**
     * 香农熵
     */
    public static double entropy(double[] p){
        double H = 0;
        for(int i = 0; i < p.length; i++) {
            H += -p[i] * (Math.log(p[i]) / Math.log(2));
        }
        return H;
    }

    /**
     * 样本均值
     * @param p
     * @return
     */
    public static double Avr(double[] p){
        double H = 0;
        for(int i = 0; i < p.length; i++) {
            H += p[i];
        }
        return H/p.length;
    }

    /**
     * 标准差
     * @param p
     * @return
     */
    public static double StandardDeviation(double[] p){
        double H = 0;
        double avr = Avr(p);//均值
        for(int i = 0; i < p.length; i++) {
            H += Math.pow((p[i]-avr),2);
        }
        return Math.sqrt(H/p.length-1);
    }
}
